<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>属性说明符序列(C23 起)</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/attributes","wgTitle":"c/language/attributes","wgCurRevisionId":73161,"wgArticleId":13241,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/language/attributes","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_attributes skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">属性说明符序列<span class="t-mark-rev t-since-c23">(C23 起)</span></h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language">C 语言</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> <a href="basic_concepts.html" title="c/language/basic concepts">基本概念</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../keyword.html" title="c/keyword">关键词</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../preprocessor.html" title="c/preprocessor">预处理器</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="statements.html" title="c/language/statements">语句</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html" title="c/language/expressions">表达式</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="initialization.html" title="c/language/initialization">初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="declarations.html" title="c/language/declarations">声明</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="functions.html" title="c/language/functions">函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> 杂项</td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="history.html" title="c/language/history">C 的历史</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="c/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="c/language/pointer">指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="c/language/array">数组</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="c/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="struct.html" title="c/language/struct"><span class="t-lines"><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="c/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bit_field.html" title="c/language/bit field">位域</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="atomic.html" title="c/language/atomic">原子类型</a> <span class="t-mark-rev t-since-c11">(C11)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="const.html" title="c/language/const"><span class="t-lines"><span>const</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="volatile.html" title="c/language/volatile"><span class="t-lines"><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="restrict.html" title="c/language/restrict"><span class="t-lines"><span>restrict</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Alignas.html" title="c/language/ Alignas"><span class="t-lines"><span>_Alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="c/language/storage duration">存储期与链接</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="extern.html" title="c/language/extern">外部及试探性定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="c/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Static_assert.html" title="c/language/ Static assert"><span class="t-lines"><span>_Static_assert</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 属性</strong> <span class="t-mark-rev t-since-c23">(C23)</span></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><strong class="selflink"> 属性</strong><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="attributes/deprecated.html" title="c/language/attributes/deprecated"><span class="t-lines"><span>deprecated</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="attributes/fallthrough.html" title="c/language/attributes/fallthrough"><span class="t-lines"><span>fallthrough</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="attributes/nodiscard.html" title="c/language/attributes/nodiscard"><span class="t-lines"><span>nodiscard</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="attributes/maybe_unused.html" title="c/language/attributes/maybe unused"><span class="t-lines"><span>maybe_unused</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div></div></td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>对类型、对象、表达式等引入实现定义的属性。
</p>
<dl><dd><code><b>[[</b></code><span class="t-spar">attr</span><code><b>]]</b></code> <code><b>[[</b></code><span class="t-spar">attr1</span>, <span class="t-spar">attr2</span>, <span class="t-spar">attr3</span><code><b>(</b></code><span class="t-spar">args</span><code><b>)</b></code><code><b>]]</b></code> <code><b>[[</b></code><span class="t-spar">属性前缀</span><code><b>::</b></code><span class="t-spar">attr</span><code><b>(</b></code><span class="t-spar">arg</span><code><b>)</b></code><code><b>]]</b></code> 
</dd></dl>
<p>正式而言，语法是
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>[[</b></code> <span class="t-spar">属性列表</span> <code><b>]]</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td> <span class="t-mark-rev t-since-c23">(C23 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>其中 <span class="t-spar">属性列表</span> 是零或多个 <span class="t-spar">属性记号</span> 的逗号分隔列表
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">标准属性</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性前缀</span> <code><b>::</b></code> <span class="t-spar">标识符</span>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">标准属性</span> <code><b>(</b></code> <span class="t-spar">实参列表</span> <code><b>)</b></code>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性前缀</span> <code><b>::</b></code> <span class="t-spar">标识符</span> <code><b>(</b></code> <span class="t-spar">实参列表</span> <code><b>)</b></code>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 标准属性，例如 <span class="t-c"><span class="mw-geshi c source-c"><span class="br0">[</span><span class="br0">[</span>fallthrough<span class="br0">]</span><span class="br0">]</span></span></span></div>
<div class="t-li1"><span class="t-li">2)</span> 有命名空间的属性，例如 <span class="t-c"><span class="mw-geshi c source-c"><span class="br0">[</span><span class="br0">[</span>gnu<span class="sy4">::</span><span class="me2">unused</span><span class="br0">]</span><span class="br0">]</span></span></span></div>
<div class="t-li1"><span class="t-li">3)</span> 有实参的标准属性，例如 <span class="t-c"><span class="mw-geshi c source-c"><span class="br0">[</span><span class="br0">[</span>deprecated<span class="br0">(</span><span class="st0">"reason"</span><span class="br0">)</span><span class="br0">]</span><span class="br0">]</span></span></span></div>
<div class="t-li1"><span class="t-li">4)</span> 有命名空间和实参列表的属性</div>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<p>属性为各种由实现定义的语言扩展（例如 GNU 与 IBM 的语言扩展 <code>__attribute__((...))</code>，微软的语言扩展 <code>__declspec()</code> 等）提供了统一化的语法。
</p><p>属性可用在 C 程序中的几乎所有位置，而且可应用于几乎所有事物：类型、变量、函数、名字、代码块、整个翻译单元，不过每个特定的属性都仅在实现所容许之处有效：<code>[[expect_true]]</code> 可能是只能与 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw1">if</span></span></span>，而非与类声明一同使用的属性，<code>[[omp::parallel()]]</code> 可能是应用到代码块或 <span class="t-c"><span class="mw-geshi c source-c"><span class="kw1">for</span></span></span> 循环，而非到类型 <code>int</code> 等的属性。（请注意这两个属性只是虚构的例子，有关标准与一些非标准属性，见下文）
</p><p>在声明中，属性可出现在整个声明之前，或直接跟在被声明实体的名字之后，这些情况下它们被组合起来。大多数其他情形中，属性应用于直接位于其之前的实体。
</p><p>不同于 C++ 中的 <a href="../../cpp/language/alignas.html" title="cpp/language/alignas"><tt>alignas</tt></a> 与 <code>[[<a href="../../cpp/language/attributes/noreturn.html" title="cpp/language/attributes/noreturn">noreturn</a>]]</code> ， <a href="_Alignas.html" title="c/language/ Alignas"><code>_Alignas</code></a> 与 <a href="_Noreturn.html" title="c/language/ Noreturn"><code>_Noreturn</code></a> 说明符不是属性说明符序列的一部分。
</p><p>两个连续的方括号记号（<code>[[</code>）只能出现于引入属性说明符之处，或在属性实参之内。
</p><p>除了以下所列出的标准属性之外，实现还可能支持任意拥有由实现定义的行为的非标准属性。所有实现所未知的属性均被忽略，且不产生错误。
</p><p>每个 <span class="t-spar">标准属性</span> 均为标准化保留。即每个非标准属性均由实现提供的 <span class="t-spar">属性前缀</span> 冠以前缀，例如 <code>[[gnu::may_alias]]</code> 与 <code>[[clang::no_sanitize]]</code> 。
</p>
<h3><span class="mw-headline" id=".E6.A0.87.E5.87.86.E5.B1.9E.E6.80.A7">标准属性</span></h3>
<p>C++ 标准仅定义下列属性。每个名字形如 <code>attr</code> 的标准属性亦能拼写成 <code>__attr__</code> 而其含义不变。
</p>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td> <code>[[<a href="attributes/deprecated.html" title="c/language/attributes/deprecated">deprecated</a>]]</code><span class="t-mark-rev t-since-c23">(C23)</span><br><code>[[deprecated("reason")]]</code><span class="t-mark-rev t-since-c23">(C23)</span>
</td>
<td>  指示允许使用声明有此属性的名称或实体，但因 <span class="t-spar">reason</span> 而不鼓励使用。
</td></tr>



<tr class="t-dsc">
<td> <code>[[<a href="attributes/fallthrough.html" title="c/language/attributes/fallthrough">fallthrough</a>]]</code><span class="t-mark-rev t-since-c23">(C23)</span>
</td>
<td> 指示从前一 case 标号直落是有意的，而在发生直落时给出警告的编译器不应该为此诊断。
</td></tr>



<tr class="t-dsc">
<td> <code>[[<a href="attributes/nodiscard.html" title="c/language/attributes/nodiscard">nodiscard</a>]]</code><span class="t-mark-rev t-since-c23">(C23)</span><br><code>[[nodiscard("reason")]]</code><span class="t-mark-rev t-since-c23">(C23)</span>
</td>
<td> 若返回值被舍弃，则鼓励编译器发布警告。
</td></tr>



<tr class="t-dsc">
<td> <code>[[<a href="attributes/maybe_unused.html" title="c/language/attributes/maybe unused">maybe_unused</a>]]</code><span class="t-mark-rev t-since-c23">(C23)</span>
</td>
<td> 抑制编译器在未使用实体上的警告，若存在。
</td></tr>

</table>
<h3><span class="mw-headline" id=".E5.B1.9E.E6.80.A7.E6.B5.8B.E8.AF.95">属性测试</span></h3>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>__has_c_attribute(</b></code> <span class="t-spar">属性记号</span> <code><b>)</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<p>检查 <span class="t-spar">属性记号</span> 所指名的属性记号的存在。
</p><p>对于标准属性，它将展开成该属性被添加到工作草案中时的年份和月份（见下表），特定于厂商的属性则以某个非零整数常量确定。
</p><p>能在 <a href="../preprocessor/conditional.html" title="c/preprocessor/conditional"><tt>
#if</tt></a> 与 <a href="../preprocessor/conditional.html" title="c/preprocessor/conditional"><tt>
#elif</tt></a> 的表达式中展开 <code>__has_c_attribute</code> 。 <a href="../preprocessor/conditional.html" title="c/preprocessor/conditional"><tt>
#ifdef</tt></a> 、 <a href="../preprocessor/conditional.html" title="c/preprocessor/conditional"><tt>
#ifndef</tt></a> 和 <a href="../preprocessor/conditional.html" title="c/preprocessor/conditional"><tt>defined</tt></a> 把它当做已定义的宏，但不能在别处使用它。
</p>
<table class="wikitable sortable">

<tr>
<th> <span class="t-spar">属性记号</span>
</th>
<th> 属性
</th>
<th> 值
</th>
<th> <span title="引入该特性的标准">标准</span>
</th></tr>
<tr>
<td><code>deprecated</code>
</td>
<td><code>[[<a href="attributes/deprecated.html" title="c/language/attributes/deprecated">deprecated</a>]]</code>
</td>
<td><span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">201904L</span></span></span>
</td>
<td><span class="t-mark-rev t-since-c23">(C23)</span>
</td></tr>
<tr>
<td><code>fallthrough</code>
</td>
<td><code>[[<a href="attributes/fallthrough.html" title="c/language/attributes/fallthrough">fallthrough</a>]]</code>
</td>
<td><span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">201904L</span></span></span>
</td>
<td><span class="t-mark-rev t-since-c23">(C23)</span>
</td></tr>
<tr>
<td><code>maybe_unused</code>
</td>
<td><code>[[<a href="attributes/maybe_unused.html" title="c/language/attributes/maybe unused">maybe_unused</a>]]</code>
</td>
<td><span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">201904L</span></span></span>
</td>
<td><span class="t-mark-rev t-since-c23">(C23)</span>
</td></tr>
<tr>
<td><code>nodiscard</code>
</td>
<td><code>[[<a href="attributes/nodiscard.html" title="c/language/attributes/nodiscard">nodiscard</a>]]</code>
</td>
<td><span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">202003L</span></span></span>
</td>
<td><span class="t-mark-rev t-since-c23">(C23)</span>
</td></tr></table>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="br0">[</span><span class="br0">[</span>gnu<span class="sy4">::</span><span class="me2">always_inline</span><span class="br0">]</span><span class="br0">]</span> <span class="br0">[</span><span class="br0">[</span>gnu<span class="sy4">::</span><span class="me2">hot</span><span class="br0">]</span><span class="br0">]</span> <span class="br0">[</span><span class="br0">[</span>gnu<span class="sy4">::</span><span class="kw4">const</span><span class="br0">]</span><span class="br0">]</span> <span class="br0">[</span><span class="br0">[</span>nodiscard<span class="br0">]</span><span class="br0">]</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明 f 带四个属性</span>
 
<span class="br0">[</span><span class="br0">[</span>gnu<span class="sy4">::</span><span class="me2">always_inline</span>, gnu<span class="sy4">::</span><span class="kw4">const</span>, gnu<span class="sy4">::</span><span class="me2">hot</span>, nodiscard<span class="br0">]</span><span class="br0">]</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 同上，但用含四个属性的单个属性说明符</span>
 
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="br0">}</span></pre></div></div>
</div>
<h3><span class="mw-headline" id=".E5.A4.96.E9.83.A8.E9.93.BE.E6.8E.A5">外部链接</span></h3>
<ul><li> <a rel="nofollow" class="external text" href="https://clang.llvm.org/docs/AttributeReference.html">Clang 中的属性</a>
</li></ul>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 3048/1000000
Preprocessor generated node count: 6755/1000000
Post‐expand include size: 68187/2097152 bytes
Template argument size: 14584/2097152 bytes
Highest expansion depth: 16/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:13241-0!*!0!!zh!*!zh!* and timestamp 20211208104748 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/attributes&amp;oldid=73161">https://zh.cppreference.com/mwiki/index.php?title=c/language/attributes&amp;oldid=73161</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/language/attributes">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年12月8日 (星期三) 02:47。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过880次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.076 secs. -->
	</body>
<!-- Cached 20211208194650 -->
</html>